package fx.mine.active.component;

import animatefx.animation.FadeOutLeft;
import io.github.palexdev.materialfx.controls.MFXRectangleToggleNode;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Log4j2
public class NavButtonContainer extends VBox {
    private final ToggleGroup toggleGroup;
    private AnchorPane currentTop;

    public NavButtonContainer() {
        this.toggleGroup = new ToggleGroup();

        setId("navBar");
        setMaxHeight(Double.MAX_VALUE);
        setMaxWidth(Double.MAX_VALUE);
        setSpacing(5.0);

        addNavItems();

        getStyleClass().add("navbar");
        getStylesheets().add(getClass().getResource("/css/navContainer.css").toExternalForm());
    }

    private void addNavItems() {
        ObservableList<Node> children = getChildren();
        List<ToggleButton> toggleButtonList = new ArrayList<>();
        toggleButtonList.add(new WyButton().create("云音乐", "/icon/Music.png"));
        toggleButtonList.add(new QQButton().create("电影", "/icon/Movie.png"));
        toggleButtonList.add(new QQButton() {
            @Override
            protected boolean isDefault() {
                return false;
            }

            @Override
            public Optional<AnchorPane> lookup() {
                return MainScene.getByPaneEnum(MainScene.ScenePaneEnum.BLOG_PANE);
            }
        }.create("博客", "/icon/blog.png"));

        toggleButtonList.add(new QQButton() {
            @Override
            protected boolean isDefault() {
                return false;
            }

            @Override
            public Optional<AnchorPane> lookup() {
                return MainScene.getByPaneEnum(MainScene.ScenePaneEnum.NOTE_PANE);
            }
        }.create("笔记", "/icon/note.png"));

        toggleButtonList.add(new QQButton() {
            @Override
            protected boolean isDefault() {
                return false;
            }

            @Override
            public Optional<AnchorPane> lookup() {
                return MainScene.getByPaneEnum(MainScene.ScenePaneEnum.BBS_PANE);
            }
        }.create("论坛", "/icon/bbs.png"));

        toggleButtonList.add(new QQButton() {
            @Override
            protected boolean isDefault() {
                return false;
            }

            @Override
            public Optional<AnchorPane> lookup() {
                return MainScene.getByPaneEnum(MainScene.ScenePaneEnum.ENGLISH_PANE);
            }
        }.create("英语", "/icon/english.png"));

        toggleButtonList.add(new QQButton() {
            @Override
            protected boolean isDefault() {
                return false;
            }

            @Override
            public Optional<AnchorPane> lookup() {
                return MainScene.getByPaneEnum(MainScene.ScenePaneEnum.NEWS_PANE);
            }
        }.create("新闻", "/icon/news.png"));

        toggleButtonList.add(new QQButton() {
            @Override
            protected boolean isDefault() {
                return false;
            }

            @Override
            public Optional<AnchorPane> lookup() {
                return MainScene.getByPaneEnum(MainScene.ScenePaneEnum.STOCK_PANE);
            }
        }.create("股票", "/icon/stock.png"));

        toggleButtonList.add(new QQButton() {
            @Override
            protected boolean isDefault() {
                return false;
            }

            @Override
            public Optional<AnchorPane> lookup() {
                return MainScene.getByPaneEnum(MainScene.ScenePaneEnum.DOUYIN_PANE);
            }
        }.create("抖音", "/icon/douyin.png"));

        toggleButtonList.add(new QQButton() {
            @Override
            protected boolean isDefault() {
                return false;
            }

            @Override
            public Optional<AnchorPane> lookup() {
                return MainScene.getByPaneEnum(MainScene.ScenePaneEnum.GITHUB_PANE);
            }
        }.create("github", "/icon/github.png"));

        toggleButtonList.add(new QQButton() {
            @Override
            protected boolean isDefault() {
                return false;
            }

            @Override
            public Optional<AnchorPane> lookup() {
                return MainScene.getByPaneEnum(MainScene.ScenePaneEnum.TRANSLATE_PANE);
            }
        }.create("翻译", "/icon/tranlate.png"));


        children.setAll(toggleButtonList);
    }

    public ToggleGroup getToggleGroup() {
        return toggleGroup;
    }

    interface ItemRegister<T, E> {
        Optional<E> lookup();

        EventHandler<ActionEvent> eventRegister();

        T create(String text, String url);
    }


    class QQButton extends WyButton {
        @Override
        protected String componentName() {
            return "电影";
        }

        @Override
        protected boolean isDefault() {
            return false;
        }

        @Override
        public Optional<AnchorPane> lookup() {
            return MainScene.getByPaneEnum(MainScene.ScenePaneEnum.NAV_PANE);
        }
    }

    class WyButton implements ItemRegister<ToggleButton, AnchorPane> {
        private ToggleButton toggleButton;

        protected String componentName() {
            return "云音乐";
        }

        protected ToggleButton getComponent() {
            return toggleButton;
        }

        protected boolean isDefault() {
            return true;
        }

        protected ToggleGroup getToggleGroup() {
            return NavButtonContainer.this.getToggleGroup();
        }

        @Override
        public Optional<AnchorPane> lookup() {
            return MainScene.getByPaneEnum(MainScene.ScenePaneEnum.MUSIC_PANE);
        }

        @Override
        public EventHandler<ActionEvent> eventRegister() {
            if (isDefault()) {
                getComponent().setSelected(true);
                lookup().ifPresent(anchorPane -> {
                    anchorPane.toFront();
                    currentTop = anchorPane;
                });

            }
            return action -> {
                Optional<AnchorPane> lookup = lookup();
                lookup.ifPresent(anchorPane -> {
                    log.info("switch {} currentTop = {}", componentName(), currentTop);
                    FadeOutLeft fadeOutLeft = new FadeOutLeft(currentTop);
                    fadeOutLeft.setResetOnFinished(true);
                    fadeOutLeft.setOnFinished(event -> {
                        getToggleGroup().getSelectedToggle().setSelected(false);
                        getComponent().setSelected(true);
                        anchorPane.toFront();
                    });
                    fadeOutLeft.play();
                    currentTop = anchorPane;
                });

            };
        }

        @Override
        public ToggleButton create(String text, String url) {
//            MFXIconWrapper wrapper = new MFXIconWrapper(description, iconSize, wrapperSize);
            ImageView imageView = new ImageView(getClass().getResource(url).toExternalForm());
            imageView.setFitHeight(32);
            imageView.setFitWidth(32);

            MFXRectangleToggleNode toggleNode = new MFXRectangleToggleNode(text, imageView);
            this.toggleButton = toggleNode;

            toggleNode.setAlignment(Pos.CENTER_LEFT);
            toggleNode.setMaxWidth(Double.MAX_VALUE);
            toggleNode.setToggleGroup(getToggleGroup());
            toggleNode.setOnAction(eventRegister());
            return toggleNode;
        }
    }
}
